package com.lanjiu.lib.business.handler;

import android.os.Handler;
import android.os.Message;

import com.lanjiu.lib.business.TransmissionConstant;
import com.lanjiu.lib.business.listener.FileTransmissionListener;

/**
 * @description: 文件接收响应处理
 * @author: Panwei
 * @date: 2019/11/2 9:42
 */
public class FileReceiverHandler extends Handler {

    private FileTransmissionListener.OnFileReceiverListener mListener;

    public FileReceiverHandler(FileTransmissionListener.OnFileReceiverListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case TransmissionConstant.HANDLER_WHAT_ON_START:
                mListener.onStart();
                break;
            case TransmissionConstant.HANDLER_WHAT_ON_PROGRESS:
                mListener.onProgress(msg.arg1);
                break;
            case TransmissionConstant.HANDLER_WHAT_ON_COMPLETE:
                mListener.onComplete(msg.obj.toString());
                break;
            case TransmissionConstant.HANDLER_WHAT_ON_ERROR:
                mListener.onError(msg.arg2, msg.obj.toString());
                break;
        }
    }

    public void sendStartMessage(){
        Message message = obtainMessage(TransmissionConstant.HANDLER_WHAT_ON_START);
        sendMessage(message);
    }

    public void sendProgressMessage(int progress) {
        Message message = obtainMessage(TransmissionConstant.HANDLER_WHAT_ON_PROGRESS);
        message.arg1 = progress;
        sendMessage(message);
    }

    public void sendCompleteMessage(String clientPath) {
        Message message = obtainMessage(TransmissionConstant.HANDLER_WHAT_ON_COMPLETE);
        message.obj = clientPath;
        sendMessage(message);
    }

    public void sendErrorMessage(int code, String msg) {
        Message message = obtainMessage(TransmissionConstant.HANDLER_WHAT_ON_ERROR);
        message.arg2 = code;
        message.obj = msg;
        sendMessage(message);
    }
}
